import { queryTagGroup } from '@/services/api';
import { useQuery } from '@tanstack/react-query';
import type { ITagGroup } from '@/interfaces';
import { useContext } from 'react';
import Alert from '@/app/[locale]/alert/alert';
import AlertLoad from '@/app/[locale]/alert/load';
import { TagGroupAdminContext } from '@/contexts/admin/forum/tagGroup';
import QueryTagGroupInfo from '@/app/[locale]/admin/forum/tagGroup/detail/info';
import QueryTagGroupTags from '@/app/[locale]/admin/forum/tagGroup/detail/tags';
import QueryTagGroupSections from '@/app/[locale]/admin/forum/tagGroup/detail/sections';
import QueryTagGroupPosts from '@/app/[locale]/admin/forum/tagGroup/detail/posts';

export default function DetailsTagGroupAdminPage() {
  const context = useContext(TagGroupAdminContext);

  const queryTagGroupQuery = useQuery(
    ['/forum', '/tagGroups', context.detailItem?.id],
    async (context) => {
      return (await queryTagGroup({
        id: context.queryKey[2] + '',
      })) as ITagGroup;
    },
  );

  if (queryTagGroupQuery.data) {
    const data = queryTagGroupQuery.data;

    return (
      <div className="row">
        <div className="col">
          <div className="vstack gap-4">
            <QueryTagGroupInfo details={data} />

            <QueryTagGroupTags details={data} />

            <div className="row">
              <div className="col">
                <QueryTagGroupSections details={data} />
              </div>
              <div className="col">
                <QueryTagGroupPosts details={data} />
              </div>
            </div>

            <div></div>
          </div>
        </div>
      </div>
    );
  }

  if (queryTagGroupQuery.error) {
    return <Alert type="error" message={queryTagGroupQuery.error} />;
  }

  return <AlertLoad />;
}
